package com.cspm.ahjy.bc.office.word.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.springframework.util.StringUtils;

import com.cspm.ahjy.bc.office.enums.ColorEnum;
import com.cspm.ahjy.bc.office.enums.PaperSizeEnum;
import com.cspm.ahjy.bc.office.util.UnitConvertUtil;
import com.cspm.ahjy.bc.office.word.domain.MSAction;
import com.cspm.ahjy.bc.office.word.domain.MSActionType;
import com.cspm.ahjy.bc.office.word.domain.MSBody;
import com.cspm.ahjy.bc.office.word.domain.MSBorder;
import com.cspm.ahjy.bc.office.word.domain.MSFont;
import com.cspm.ahjy.bc.office.word.domain.MSPaperSize;
import com.cspm.ahjy.bc.office.word.domain.MSParagraph;
import com.cspm.ahjy.bc.office.word.domain.MSPicture;
import com.cspm.ahjy.bc.office.word.domain.MSTable;
import com.cspm.ahjy.bc.office.word.domain.MSWordart;

/**
 * 正文处理（可包含标题）
 * 
 * @author ServerZhang
 * @date 2017年9月28日
 */
public class BodyUtil {

	/**
	 * 解析<w:body>标签，获取文档Body对象 当文档中出现分栏等操作后，会存在多个<wx:sect>标签：
	 * （1）分栏后的段落会被分隔在独立的<wx:sect>标签中（例如分栏了一个段落，则整个body中就有3个<wx:sect>标签）
	 * （2）所有的<wx:sect>标签中只存在一个<w:sectPr>标签以存储整个页面的样式信息
	 * （3）不幸的是，原本位于<w:sectPr>标签下的页眉和页脚会以<w:p>的身份被分配到第1个<wx:sect>中！！！
	 * （4）被分栏的段落后会出现一个新的空段落<w:p>，并且在<w:pPr>下会存在<w:sectPr>存储分栏信息
	 * 
	 * @param sectElement
	 * @param path 文件所在路径
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月29日
	 */
	public static MSBody getMsBody(Element bodyElement, String path) {
		MSBody msBody = new MSBody();
		
		//文档背景色
		List<Element> selectNodes = bodyElement.selectNodes("//w:bgPict");
		if (selectNodes.size() > 0) {
			Element bgElement = selectNodes.get(0);
			if (bgElement != null) {
				Element bg = bgElement.element("background");
				if (bg != null) {
					Attribute bgcolor = bg.attribute("bgcolor");
					if (bgcolor != null) {
						msBody.setBgColor(bgcolor.getText());
					}
				}
			}
		}
		
		List<Element> sectElements = bodyElement.elements("sect");

		if (sectElements.size() < 1) {
			return msBody;
		}
		// 从最后一个
		Element sectElement = sectElements.get(sectElements.size() - 1);

		// 1.获取页面属性
		Element sectPrE = sectElement.element("sectPr");
		if (sectPrE != null) {
			Element pgSzE = sectPrE.element("pgSz");
			if (pgSzE != null) {
				MSPaperSize msPaperSize = PageSizeUtil.getMsPaperSize(pgSzE);

				if (msPaperSize != null) {
					msBody.setPaperSize(msPaperSize);
				}

				Attribute orientAttribute = pgSzE.attribute("orient");
				if (orientAttribute != null) {
					msBody.setOrient(orientAttribute.getText());
				}
			}

			Element pgBordersE = sectPrE.element("pgBorders");
			if (pgBordersE != null) {

				String topType = null;
				String leftType = null;
				String bottomType = null;
				String rightType = null;

				Integer topWidth = null;
				Integer leftWidth = null;
				Integer bottomWidth = null;
				Integer rightWidth = null;

				String topColor = null;
				String leftColor = null;
				String bottomColor = null;
				String rightColor = null;
				
				Boolean topShadow = null;
				Boolean leftShadow = null;
				Boolean bottomShadow = null;
				Boolean rightShadow = null;

				Element topE = pgBordersE.element("top");
				if (topE != null) {
					MSBorder msBorder = BorderUtil.getMsBorder(topE);
					msBody.setTopBorder(msBorder);
					topType = msBorder.getType();
					topWidth = msBorder.getWidth();
					topColor = msBorder.getColor();
					topShadow = msBorder.getShadow();
				}
				Element leftE = pgBordersE.element("left");
				if (leftE != null) {
					MSBorder msBorder = BorderUtil.getMsBorder(leftE);
					msBody.setLeftBorder(msBorder);
					leftType = msBorder.getType();
					leftWidth = msBorder.getWidth();
					leftColor = msBorder.getColor();
					leftShadow = msBorder.getShadow();
				}
				Element bottomE = pgBordersE.element("bottom");
				if (bottomE != null) {
					MSBorder msBorder = BorderUtil.getMsBorder(bottomE);
					msBody.setBottomBorder(msBorder);
					bottomType = msBorder.getType();
					bottomWidth = msBorder.getWidth();
					bottomColor = msBorder.getColor();
					bottomShadow = msBorder.getShadow();
				}
				Element rightE = pgBordersE.element("right");
				if (rightE != null) {
					MSBorder msBorder = BorderUtil.getMsBorder(rightE);
					msBody.setRightBorder(msBorder);
					rightType = msBorder.getType();
					rightWidth = msBorder.getWidth();
					rightColor = msBorder.getColor();
					rightShadow = msBorder.getShadow();
				}

				if (topType != null && leftType != null && bottomType != null
						&& rightType != null && topType.equals(leftType)
						&& topType.equals(bottomType)
						&& topType.equals(rightType)) {
					msBody.setBoderType(topType);
				}

				if (topWidth != null && leftWidth != null
						&& bottomWidth != null && rightWidth != null
						&& !topWidth.equals(0) && topWidth.equals(leftWidth)
						&& topWidth.equals(bottomWidth)
						&& topWidth.equals(rightWidth)) {
					msBody.setBoderWidth(topWidth);
				}

				if (topColor != null && leftColor != null
						&& bottomColor != null && rightColor != null
						&& topColor.equals(leftColor)
						&& topColor.equals(bottomColor)
						&& topColor.equals(rightColor)) {
					msBody.setBoderColor(topColor);
				}
				
				if (topShadow != null && leftShadow != null
						&& bottomShadow != null && rightShadow != null
						&& topShadow.equals(leftShadow)
						&& topShadow.equals(bottomShadow)
						&& topShadow.equals(rightShadow)) {
					msBody.setBoderShadow(topShadow);
				}
			}

			// 页眉和水印
			Element hdrE = sectPrE.element("hdr");
			if (hdrE != null) {
				// 这种方式获取不稳定，不采用
			}

			// 页脚
			Element ftrE = sectPrE.element("ftr");
			if (ftrE != null) {
				// 这种方式获取不稳定，不采用
			}

			Element pgMarE = sectPrE.element("pgMar");
			if (pgMarE != null) {
				Attribute topAttribute = pgMarE.attribute("top");
				if (topAttribute != null) {
					msBody.setTop(Integer.parseInt(topAttribute.getText()));
				}
				Attribute rightAttribute = pgMarE.attribute("right");
				if (rightAttribute != null) {
					msBody.setRight(Integer.parseInt(rightAttribute.getText()));
				}
				Attribute bottomAttribute = pgMarE.attribute("bottom");
				if (bottomAttribute != null) {
					msBody.setBottom(Integer.parseInt(bottomAttribute.getText()));
				}
				Attribute leftAttribute = pgMarE.attribute("left");
				if (leftAttribute != null) {
					msBody.setLeft(Integer.parseInt(leftAttribute.getText()));
				}
				Attribute gutterAttribute = pgMarE.attribute("gutter");
				if (gutterAttribute != null) {
					msBody.setGutter(Integer.parseInt(gutterAttribute.getText()));
				}
				Attribute headerAttribute = pgMarE.attribute("header");
				if (headerAttribute != null) {
					msBody.setHeader(Integer.parseInt(headerAttribute.getText()));
				}
				Attribute footerAttribute = pgMarE.attribute("footer");
				if (footerAttribute != null) {
					msBody.setFooter(Integer.parseInt(footerAttribute.getText()));
				}
			}
		}

		// 2.获取所有段,不包含页眉、页脚和水印
		// 3.获取所有表格
		// Map<String, List<MSParagraph>> map =
		// ParagraphUtil.getAllParagraph(bodyElement);

		Iterator<Element> iterator = sectElements.iterator();
		List<MSParagraph> paragraphs = new ArrayList<MSParagraph>();
		List<MSTable> tables = new ArrayList<MSTable>();
		while (iterator.hasNext()) {
			Element next = iterator.next();// <wx:sect>

			Map<String, List<MSParagraph>> map = ParagraphUtil.getParagraphs(
					next, paragraphs, 0, path);

			/*
			 * List<MSParagraph> headers = map.get("header"); List<MSParagraph>
			 * footers = map.get("footer");
			 */
			List<MSParagraph> list = map.get("paragraph");
			paragraphs.addAll(list);

			List<MSTable> msTables = TableUtil.getMsTables(next, path);
			tables.addAll(msTables);

		}

		// 4.页眉、页脚、水印
		Map<String, MSFont> specials = getSpecials(bodyElement, path);
		msBody.setHeaderFont(specials.get("header"));
		msBody.setFooterFont(specials.get("footer"));
		msBody.setWatermark(specials.get("watermark"));

		msBody.addParagraph(paragraphs);
		msBody.setTables(tables);

		return msBody;
	}

	/**
	 * 返回指定段落中的指定文字
	 * 
	 * @param body
	 * @param paragraphNums
	 *            指定段落
	 * @param text
	 *            指定文字，为空时表示获取全段文字
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月28日
	 */
	public static List<MSFont> getFonts(MSBody body,
			List<Integer> paragraphNums, String text) {
		List<MSFont> returnFonts = new ArrayList<MSFont>();

		if (null == body) {
			return returnFonts;
		}
		List<MSParagraph> paragraphs = body.getParagraphs();
		int size = paragraphs.size();

		for (Integer num : paragraphNums) {
			if (num >= size) {
				continue;
			}
			MSParagraph msParagraph = paragraphs.get(num);
			List<MSFont> fonts = ParagraphUtil.getFonts(msParagraph, text);
			if (fonts.size() > 0) {
				returnFonts.addAll(fonts);
			}
		}
		return returnFonts;
	}

	/**
	 * 正文实体比较差异
	 * 
	 * @param msBody1
	 * @param msBody2
	 * @author ServerZhang
	 * @date 2017年9月28日
	 */
	public static List<MSAction> compare(MSBody msBody1, MSBody msBody2) {
		List<MSAction> actions = new ArrayList<MSAction>();

		// 1.Body属性比较
		List<MSAction> compareBodyPro = compareProperty(msBody1, msBody2);
		if (compareBodyPro != null && compareBodyPro.size() > 0) {
			actions.addAll(compareBodyPro);
		}

		// 2.段落比较
		List<MSParagraph> paragraphs1 = msBody1.getParagraphs();
		List<MSParagraph> paragraphs2 = msBody2.getParagraphs();

		int size1 = paragraphs1.size();
		int size2 = paragraphs2.size();

		// 判断是否有整段换位
		for (int j = 0; j < size2; j++) {
			MSParagraph msParagraph2 = paragraphs2.get(j);
			for (int i = 0; i < size1; i++) {
				MSParagraph msParagraph1 = paragraphs1.get(i);
				if (msParagraph1.maybeEquals(msParagraph2)) {// 判断是同一段
					if (i + 1 == j) {// 段落位置调换了，仅支持相邻段落
						MSParagraph msParagraph11 = paragraphs1.get(i + 1);
						MSParagraph msParagraph22 = paragraphs2.get(j - 1);
						if (msParagraph11.maybeEquals(msParagraph22)) {// 证实相邻段落调换位置

							String text1 = msParagraph1.getText();
							int length1 = text1.length();
							String subStr1 = length1 > 12 ? text1.substring(0,
									12) : text1.substring(0, length1);// 取段首12字作比对，相同则认为是同一段落

							String text11 = msParagraph11.getText();
							int length11 = text11.length();
							String subStr11 = length11 > 12 ? text11.substring(
									0, 12) : text11.substring(0, length11);// 取段首12字作比对，相同则认为是同一段落
							MSAction action = new MSAction();
							MSActionType actionType = new MSActionType();
							actionType.setType("body_paragraph");
							Integer[] value = { i, j };
							actionType.setTypeValue(value);
							actionType.setTypeContent("调换第" + i + "段（"
									+ subStr1 + "...）和第" + j + "段（" + subStr11
									+ "...）的位置");
							action.setMsActionType(actionType);
							actions.add(action);
						}
					}
					List<MSAction> compare = ParagraphUtil.compare(
							msParagraph1, msParagraph2, j);
					if (compare != null && compare.size() > 0) {
						actions.addAll(compare);
					}
				} else if (j == size1 && j == i + 1) {// 说明是新增段落
					List<MSAction> addParagraph = ParagraphUtil.addParagraph(
							msParagraph2, j);
					if (addParagraph != null && addParagraph.size() > 0) {
						actions.addAll(addParagraph);
					}
				}
			}
		}

		// 3.表格比较
		List<MSTable> tables1 = msBody1.getTables();
		List<MSTable> tables2 = msBody2.getTables();
		int tSize1 = tables1.size();
		int tSize2 = tables2.size();

		for (int i = 0; i < tSize1 && i < tSize2; i++) {
			MSTable msTable1 = tables1.get(i);
			MSTable msTable2 = tables2.get(i);

			List<MSAction> compare = TableUtil.compare(msTable1, msTable2, i);
			if (compare != null && compare.size() > 0) {
				actions.addAll(compare);
			}
		}

		return actions;
	}

	/**
	 * 页面属性比较
	 * 
	 * @param msBody1
	 * @param msBody2
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月13日
	 */
	public static List<MSAction> compareProperty(MSBody msBody1, MSBody msBody2) {
		List<MSAction> actions = new ArrayList<MSAction>();

		// 页边距
		Integer top1 = msBody1.getTop();
		Integer top2 = msBody2.getTop();
		Integer bottom1 = msBody1.getBottom();
		Integer bottom2 = msBody2.getBottom();
		Integer left1 = msBody1.getLeft();
		Integer left2 = msBody2.getLeft();
		Integer right1 = msBody1.getRight();
		Integer right2 = msBody2.getRight();
		Integer gutter1 = msBody1.getGutter();
		Integer gutter2 = msBody2.getGutter();
		String orient1 = msBody1.getOrient();
		String orient2 = msBody2.getOrient();
		String bgColor1 = msBody1.getBgColor();
		String bgColor2 = msBody2.getBgColor();

		if (bgColor2 != null && !bgColor2.equals(bgColor1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_bgColor");
			actionType.setTypeValue(bgColor2);
			
			String content = "背景颜色“";
			ColorEnum byValue = ColorEnum.getByValue(bgColor2);
			if (byValue != null) {
				content += byValue.getName();
			}else {
				content += bgColor2;
			}
			content += "”";
			
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		
		if (!top1.equals(top2)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_top");
			actionType.setTypeValue(top2);
			actionType.setTypeContent("上边距" + UnitConvertUtil.standar2CM(top2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!bottom1.equals(bottom2)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_bottom");
			actionType.setTypeValue(bottom2);
			actionType.setTypeContent("下边距"
					+ UnitConvertUtil.standar2CM(bottom2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!left1.equals(left2)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_left");
			actionType.setTypeValue(left2);
			actionType
					.setTypeContent("左边距" + UnitConvertUtil.standar2CM(left2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!right1.equals(right2)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_right");
			actionType.setTypeValue(right2);
			actionType.setTypeContent("右边距"
					+ UnitConvertUtil.standar2CM(right2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!gutter1.equals(gutter2)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_gutter");
			actionType.setTypeValue(gutter2);
			actionType.setTypeContent("装订线"
					+ UnitConvertUtil.standar2CM(gutter2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!orient1.equals(orient2)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_orient");
			actionType.setTypeValue(orient2);
			actionType.setTypeContent("方向为"
					+ ("landscape".equals(orient2) ? "横向" : "纵向"));
			action.setMsActionType(actionType);
			actions.add(action);
		}

		// 纸张
		MSPaperSize paperSize1 = msBody1.getPaperSize();
		MSPaperSize paperSize2 = msBody2.getPaperSize();
		if (paperSize2 != null && !paperSize2.equals(paperSize1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_paperSize");
			actionType.setTypeValue(paperSize2);
			String content = "纸张大小设为“";

			Integer width = paperSize2.getWidth();
			Integer height = paperSize2.getHeight();
			PaperSizeEnum bySize = PaperSizeEnum.getBySize(width, height);
			if (bySize != null) {
				content += bySize.getPaperName();
			} else {
				content += "宽度" + UnitConvertUtil.standar2CM(width) + "，高度"
						+ UnitConvertUtil.standar2CM(height);
			}
			content += "”";
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		// 页眉、页脚距边界
		Integer footer1 = msBody1.getFooter();
		Integer footer2 = msBody2.getFooter();
		Integer header1 = msBody1.getHeader();
		Integer header2 = msBody2.getHeader();
		if (!header2.equals(header1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_header");
			actionType.setTypeValue(header2);
			actionType.setTypeContent("页眉距边界"
					+ UnitConvertUtil.standar2CM(header2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!footer2.equals(footer1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_footer");
			actionType.setTypeValue(footer2);
			actionType.setTypeContent("页脚距边界"
					+ UnitConvertUtil.standar2CM(footer2));
			action.setMsActionType(actionType);
			actions.add(action);
		}

		// 页面边框
		// 上下左右边框的属性一致时才比较
		/*String boderType1 = msBody1.getBoderType();
		String boderType2 = msBody2.getBoderType();
		if (boderType2 != null && !boderType2.equals(boderType1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_boderType");
			actionType.setTypeValue(boderType2);
			actionType.setTypeContent("页面边框，线型为“" + boderType2 + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}*/
		Boolean boderShadow1 = msBody1.getBoderShadow();
		Boolean boderShadow2 = msBody2.getBoderShadow();
		if (boderShadow2 != null && !boderShadow2.equals(boderShadow1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_boderShadow");
			actionType.setTypeValue(boderShadow2);
			actionType.setTypeContent("页面边框，样式为“" + (boderShadow2?"阴影样式":"方框或三维样式") + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}
		Integer boderWidth1 = msBody1.getBoderWidth();
		Integer boderWidth2 = msBody2.getBoderWidth();
		if (boderWidth2 != null && !boderWidth2.equals(0)
				&& !boderWidth2.equals(boderWidth1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_boderWidth");
			actionType.setTypeValue(boderWidth2);
			actionType.setTypeContent("页面边框，宽度为“"
					+ UnitConvertUtil.standar2PT(boderWidth2) + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}
		String boderColor1 = msBody1.getBoderColor();
		String boderColor2 = msBody2.getBoderColor();
		if (boderColor2 != null && !boderColor2.equals(boderColor1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_boderColor");
			actionType.setTypeValue(boderColor2);
			
			String content = "页面边框，颜色为“";
			ColorEnum byValue = ColorEnum.getByValue(boderColor2);
			if (byValue != null) {
				content += byValue.getName();
			}else {
				content += boderColor2;
			}
			content += "”";
			
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		// 页眉
		MSFont headerFont1 = msBody1.getHeaderFont();
		MSFont headerFont2 = msBody2.getHeaderFont();
		if (headerFont2 != null) {
			List<MSAction> addHeader = addHeader(headerFont2);
			if (addHeader != null && addHeader.size() > 0) {
				actions.addAll(addHeader);
			}
		}
		// 页脚
		MSFont footerFont1 = msBody1.getFooterFont();
		MSFont footerFont2 = msBody2.getFooterFont();
		if (footerFont2 != null) {
			List<MSAction> addFooter = addFooter(footerFont2);
			if (addFooter != null && addFooter.size() > 0) {
				actions.addAll(addFooter);
			}
		}
		// 水印
		MSFont watermark1 = msBody1.getWatermark();
		MSFont watermark2 = msBody2.getWatermark();
		if (watermark2 != null) {
			List<MSAction> addWatermark = addWatermark(watermark2);
			if (addWatermark != null && addWatermark.size() > 0) {
				actions.addAll(addWatermark);
			}
		}

		return actions;
	}

	/**
	 * 路径表达式，获取页眉、页脚、水印
	 * 
	 * @param bodyElement
	 * @param path 文档所在目录
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月13日
	 */
	public static Map<String, MSFont> getSpecials(Element bodyElement, String path) {
		Map<String, MSFont> map = new HashMap<String, MSFont>();

		MSFont header = null;
		MSFont footer = null;
		MSFont watermark = null;

		List<Element> hdrs = bodyElement.selectNodes("//w:hdr");
		List<Element> ftrs = bodyElement.selectNodes("//w:ftr");

		Iterator<Element> iterator = hdrs.iterator();
		while (iterator.hasNext()) {
			Element next = iterator.next();
			Attribute typeA = next.attribute("type");
			if (typeA != null) {
				String type = typeA.getText();
				if ("even".equals(type)) {// 水印

					watermark = getSpecialFont(next,path);

				} else if ("odd".equals(type)) {// 页眉

					MSFont specialFont = getSpecialFont(next,path);
					if (specialFont != null) {
						MSWordart msWordart = specialFont.getMsWordart();
						MSPicture msPicture = specialFont.getMsPicture();
						
						if (msWordart == null && msPicture == null) {
							header = specialFont;
						}
					}
				}
			}
		}

		Iterator<Element> ftrsIterator = ftrs.iterator();
		while (ftrsIterator.hasNext()) {
			Element next = ftrsIterator.next();
			Attribute typeA = next.attribute("type");
			if (typeA != null) {
				String type = typeA.getText();
				if ("odd".equals(type)) {// 页脚

					footer = getSpecialFont(next,path);

					break;// 仅有一个，不再循环
				}
			}
		}

		map.put("header", header);
		map.put("footer", footer);
		map.put("watermark", watermark);

		return map;
	}

	/**
	 * 迭代解析<w:hdr>或<w:ftr>标签下的p标签 从第一p标签下解析第一个r标签
	 * 
	 * @param specialE
	 * @param path 文档所在目录
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月13日
	 */
	private static MSFont getSpecialFont(Element specialE,String path) {
		MSFont msFont = null;

		List<Element> elements = specialE.elements();
		Iterator<Element> iterator = elements.iterator();
		while (iterator.hasNext()) {
			Element next = iterator.next();
			String name = next.getName();
			if ("p".equals(name)) {
				List<Element> rElements = next.elements("r");
				if (rElements.size() > 0) {
					Element rElement = rElements.get(rElements.size() - 1);// 获取最后一个r标签

					msFont = FontUtil.getFont(rElement, 0, path);
				}
				return msFont;
			} else if ("borders".equals(name)) {
				continue;
			} else {
				msFont = getSpecialFont(next,path);
				return msFont;
			}
		}

		return msFont;
	}

	/**
	 * 添加页眉
	 * 
	 * @param headerFont
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月13日
	 */
	public static List<MSAction> addHeader(MSFont headerFont) {
		List<MSAction> actions = new ArrayList<MSAction>();

		String text = headerFont.getText();
		if (!StringUtils.isEmpty(text)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_header_text");
			actionType.setTypeValue(text);
			actionType.setTypeContent("设置页眉，文字为“" + text + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}

		String fontName = headerFont.getFontName();
		if (!StringUtils.isEmpty(fontName)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_header_fontName");
			actionType.setTypeValue(fontName);
			actionType.setTypeContent("设置页眉，字体为“" + fontName + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}

		Integer fontSize = headerFont.getFontSize();
		if (!StringUtils.isEmpty(fontSize) && !fontSize.equals(0)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_header_fontSize");
			actionType.setTypeValue(fontSize);
			actionType.setTypeContent("设置页眉，字号为“" + fontSize + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}

		String color = headerFont.getColor();
		if (!StringUtils.isEmpty(color)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_header_color");
			actionType.setTypeValue(color);
			
			String content = "设置页眉，文字颜色为“";
			ColorEnum byValue = ColorEnum.getByValue(color);
			if (byValue != null) {
				content += byValue.getName();
			}else {
				content += color;
			}
			content += "”";
			
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		return actions;
	}

	/**
	 * 添加页脚
	 * 
	 * @param footerFont
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月13日
	 */
	public static List<MSAction> addFooter(MSFont footerFont) {
		List<MSAction> actions = new ArrayList<MSAction>();

		String text = footerFont.getText();
		if (!StringUtils.isEmpty(text)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_footer_text");
			actionType.setTypeValue(text);
			actionType.setTypeContent("设置页脚，文字为“" + text + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}

		String fontName = footerFont.getFontName();
		if (!StringUtils.isEmpty(fontName)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_footer_fontName");
			actionType.setTypeValue(fontName);
			actionType.setTypeContent("设置页脚，字体为“" + fontName + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}

		Integer fontSize = footerFont.getFontSize();
		if (!StringUtils.isEmpty(fontSize) && !fontSize.equals(0)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_footer_fontSize");
			actionType.setTypeValue(fontSize);
			actionType.setTypeContent("设置页脚，字号为“" + fontSize + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}

		String color = footerFont.getColor();
		if (!StringUtils.isEmpty(color)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_footer_color");
			actionType.setTypeValue(color);
			
			String content = "设置页脚，文字颜色为“";
			ColorEnum byValue = ColorEnum.getByValue(color);
			if (byValue != null) {
				content += byValue.getName();
			}else {
				content += color;
			}
			content += "”";
			
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		return actions;
	}

	/**
	 * 添加水印
	 * 
	 * @param footerFont
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月13日
	 */
	public static List<MSAction> addWatermark(MSFont watermark) {
		List<MSAction> actions = new ArrayList<MSAction>();

		MSPicture msPicture = watermark.getMsPicture();
		MSWordart msWordart = watermark.getMsWordart();

		if (msPicture != null) {
			return addWatermark4Pic(msPicture);
		} else if (msWordart == null) {
			return actions;
		}

		String text = msWordart.getText();
		if (!StringUtils.isEmpty(text)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_watermark_text_text");
			actionType.setTypeValue(text);
			actionType.setTypeContent("添加文字水印，内容为“" + text + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}

		String fontName = msWordart.getFontName();
		if (!StringUtils.isEmpty(fontName)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_watermark_text_fontName");
			actionType.setTypeValue(fontName);
			actionType.setTypeContent("添加文字水印，字体为“" + fontName + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}

		
		/*Integer fontSize = msWordart.getFontSize();
		if (!StringUtils.isEmpty(fontSize) && !fontSize.equals(0)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_watermark_text_fontSize");
			actionType.setTypeValue(fontSize);
			actionType.setTypeContent("添加文字水印，字号为“" + fontSize + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}*/
		 

		String color = msWordart.getColor();
		if (!StringUtils.isEmpty(color)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_watermark_text_color");
			actionType.setTypeValue(color);
			
			String content = "添加文字水印，文字颜色为“";
			ColorEnum byValue = ColorEnum.getByValue(color);
			if (byValue != null) {
				content += byValue.getName();
			}else {
				content += color;
			}
			content += "”";
			
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		return actions;
	}

	/**
	 * 添加图片水印
	 * 
	 * @param picture
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月13日
	 */
	public static List<MSAction> addWatermark4Pic(MSPicture picture) {
		List<MSAction> actions = new ArrayList<MSAction>();

		String title = picture.getTitle();
		if (!StringUtils.isEmpty(title)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_watermark_picture_title");
			actionType.setTypeValue(title);
			actionType.setTypeContent("添加图片水印，标题为“" + title + "”");
			action.setMsActionType(actionType);
			actions.add(action);
		}
		Boolean ablation = picture.getAblation();
		if (ablation) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_watermark_picture_ablation");
			actionType.setTypeValue(ablation);
			actionType.setTypeContent("添加图片水印，有“冲蚀”效果");
			action.setMsActionType(actionType);
			actions.add(action);
		}
		Double zoomWidth = picture.getZoomWidth();
		if (zoomWidth != null) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("body_watermark_picture_zoomWidth");
			actionType.setTypeValue(zoomWidth);
			actionType.setTypeContent("添加图片水印，缩放比例为“"+(zoomWidth*100)+"%”");
			action.setMsActionType(actionType);
			actions.add(action);
		}

		return actions;
	}
}
